﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Validation;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;

namespace KPIS.GERP.WCF.ePayment.Models
{
    [DataContract, Table("Invoice")]
    public class Invoice
    {
        public Invoice()
        {
            deleted = false;
            this.InvoiceItems = new HashSet<InvoiceItem>();
        }

        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [DataMember, Display(Name = "invoice_id")]
        public int invoice_id { get; set; }

        [DataMember, Display(Name = "เลขที่ใบ invoice")]
        [MaxLength(25)]
        [Required]
        public string invoice_code { get; set; }

        [DataMember, Display(Name = "เลขที่รายการสั่งซื้อ/สัญญา")]
        [MaxLength(25)]
        [Required]
        public string po_code { get; set; }

        [DataMember, Display(Name = "งวดที่")]
        [MaxLength(20)]
        [Required]
        public string lot_no { get; set; }

        [DataMember, Display(Name = "วันที่")]
        public DateTime date { get; set; }

        [ForeignKey("Supplier")]
        [DataMember, Display(Name = "ผู้ขาย")]
        public int supplier_id { get; set; }

        [DataMember, Display(Name = "doc_id")]
        public string doc_id { get; set; }


        [DataMember, Display(Name = "วันที่สร้างรายการ")]
        [Required]
        public DateTime created_when { get; set; }

        [DataMember, Display(Name = "วันที่แก้ไขรายการ")]
        public DateTime? updated_when { get; set; }

        [DataMember, Display(Name = "ลบ")]
        public bool deleted { get; set; }

        [DataMember, Display(Name = "วันที่ลบรายการ")]
        public DateTime? deleted_when { get; set; }

        public virtual Supplier Supplier { get; set; }

        public virtual ICollection<InvoiceItem> InvoiceItems { get; set; }

        public int Create(Invoice model)
        {
            using(ePaymentContext context = new ePaymentContext())
            {
                try
                {
                    model.deleted = false;
                    model.created_when = DateTime.Now;

                    context.Invoices.Add(model);
                    context.SaveChanges();

                    return model.invoice_id;
                }
                catch (DbEntityValidationException ex)
                {
                    throw ex;
                }
            }
        }

        public void Edit(Invoice model)
        {
            using (ePaymentContext context = new ePaymentContext())
            {
                try
                {
                    model.updated_when = DateTime.Now;

                    context.Entry(model).State = System.Data.Entity.EntityState.Modified;
                    context.SaveChanges();
                }
                catch (DbEntityValidationException ex)
                {
                    throw ex;
                }
            }
        }

        public void Delete(Invoice model)
        {
            using (ePaymentContext context = new ePaymentContext())
            {
                try
                {
                    model.deleted_when = DateTime.Now;
                    model.deleted = true;

                    context.Entry(model).State = System.Data.Entity.EntityState.Modified;
                    context.SaveChanges();
                }
                catch (DbEntityValidationException ex)
                {
                    throw ex;
                }
            }
        }
    }
}